import { safeIndexApi } from '@/api';
import { Form, InputNumber, Spin } from 'antd';
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { SimpleFormProps, TSimpleRef } from './interface';

const rules = (text: string) => {
  return [{ required: true, message: `请输入${text}` }];
};

const SimpleFormlaSet = forwardRef<TSimpleRef, SimpleFormProps>((props, ref) => {
  const [form] = Form.useForm();
  const [loading, setloading] = useState(true);
  const { type, factorId } = props;
  const onSubmit = () => {
    return form
      .validateFields()
      .then((val) => {
        const finalValues = { ...val, type };
        if (type === 'edit') {
          finalValues.factorId = factorId;
        }
        return Promise.resolve(finalValues);
      })
      .catch((err) => {
        // message.error(' SimpleFormlaSet ERR ');
        // return Promise.reject(err); // 这东西会报错UNcaught (in promise) {values: {…}, errorFields: Array(1), outOfDate: false}
        return console.log(JSON.stringify(err));
      });
  };
  const getSimpleDetail = () => {
    setloading(true);
    safeIndexApi.simpRulesDetail({ factorId: factorId }).then((res: any) => {
      console.log(res, '打印看看啥东东');
      setloading(false);
      if (res.code == 200) {
        form.setFieldsValue({ maximumScore: res.data?.maximumScore });
      }
    });
  };
  useEffect(() => {
    if (type === 'edit' && factorId) {
      // form.setFieldsValue({ groupName });
      getSimpleDetail();
    }
  }, [type]);

  useImperativeHandle(ref, () => ({
    onSubmit,
  }));

  return (
    <Spin spinning={loading} style={{ padding: '16px 16px 0' }}>
      {/* <Form layout="horizontal" form={form}>
        <Form.Item label="规则设置">
          -
          <Form.Item
            name="numerator"
            rules={[{ required: true }]}
            style={{ display: 'inline-block', width: 100, margin: '0 0 0 5px' }}
          >
            <InputNumber min={0} max={99} placeholder="指数分子" />
          </Form.Item>
          /
          <Form.Item
            name="denominator"
            rules={[{ required: true }]}
            style={{ display: 'inline-block', width: 100, margin: '0 8px' }}
          >
            <InputNumber min={0} max={99} placeholder="请输入" />
          </Form.Item>
        </Form.Item>
      </Form> */}
      <Form layout="horizontal" form={form}>
        <Form.Item name="maximumScore" label="最高分" rules={rules('简单规则的最高分')}>
          <InputNumber min={1} max={9999} precision={0} style={{ width: 100 }} placeholder="请输入" />
        </Form.Item>
      </Form>
    </Spin>
  );
});

export default SimpleFormlaSet;
